package com.har.modules.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.har.common.exception.RRException;
import com.har.common.utils.AESUtil;
import com.har.common.utils.R;
import com.har.common.utils.ValidatorUtils;
import com.har.modules.web.entity.UserBackEntity;
import com.har.modules.web.entity.UserOrCompanyEntity;
import com.har.modules.web.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * 企业
 *
 * @author maorui
 *
 */
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserOrCompanyEntity company) throws ParseException {
        ValidatorUtils.validateEntity(company);
        companyService.updateOfBack(company);//全部更新

        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info")
    public R info(HttpServletRequest request) throws Exception {
        Cookie[] cookies=request.getCookies();
        String data=null;
        JSONObject respJson=null;
        for (Cookie cookie:cookies){
            if(null!=cookie){
                if(cookie.getName().equals("login")){
                    System.out.println(cookie.getValue());
                    data = AESUtil.getInstance().decrypt(cookie.getValue());
                    respJson = JSONObject.parseObject(data);
                }
            }
        }
        if(data==null){
            throw new RRException("查询失败");
        }
        Long id=Long.valueOf(respJson.getString("id"));
        UserBackEntity company = companyService.selectById(id);
        return R.ok().put("data", company);
    }

}
